<%@ tag language="java" pageEncoding="UTF-8"%>
<%@ include file="/WEB-INF/views/include/taglib.jsp"%>
<%@ attribute name="id" type="java.lang.String" required="true" description="编号"%>
<%@ attribute name="name" type="java.lang.String" required="true" description="输入框名称"%>
<%@ attribute name="value" type="java.lang.String" required="true" description="角色名称"%>
<%@ attribute name="userId" type="java.lang.String" required="true" description="选中内容"%>
<%@ attribute name="roleIdList" type="java.util.List" required="true" description="所选内容"%>
<%@ attribute name="allRoles" type="java.util.List" required="true" description="全部内容"%>

	<input id="${id}RoleLabel" value="${not empty value?value:''}"  htmlEscape="false"   class="required" readonly="true" style="border-radius: 3px;"/>&nbsp;
	<span class="help-inline"><font color="red">*</font> </span>
	<%-- <label id="${id}RoleLabel">${not empty value?value:'无'}</label>&nbsp; --%>
	<input id="${id}checks" name="${name}" type="hidden" value="${roleIdList}"/>
	<input id="useId" value="${userId}" type="hidden">
	<a id="${id}Button" href="javascript:" class="btn">选择</a>&nbsp;&nbsp;
<script type="text/javascript">


	$("#${id}Button").click(function(){
		top.$.jBox.open("iframe:${ctx}/sys/user/roleselect?userId="+$("#useId").val(), "选择角色", 700, $(top.document).height()-180, {
            buttons:{"确定":"ok", "清除":"clear", "关闭":true}, submit:function(v, h, f){
                if (v=="ok"){
                	var result="";
                	var contentWindow=h.find("iframe")[0].contentWindow;
                	var len= contentWindow.$("input[name='roleIdList']").length;
                	var checks=contentWindow.$("input[name='roleIdList']");
                	var labels=contentWindow.$("label");
                	var idArray = "";
                	for(var i=0;i<len;i++){
                		if(checks[i].checked){
                			result=result+labels[i].innerHTML+",";
                			idArray=idArray+checks[i].value+",";
                		}
                	}
                	if(result!=""){
                		result=result.substring(0,result.length-1);
                		idArray=idArray.substring(0,idArray.length-1);
                		$("#${id}RoleLabel").val(result);
                		$("#${id}checks").val(idArray);
                		
                	}else{
                		$("#${id}RoleLabel").val("");
                		$("#${id}checks").val("");
                	}
                	$("#${id}RoleLabel").valid();
                }else if (v=="clear"){
                	var contentWindow=h.find("iframe")[0].contentWindow;
                	var checks=contentWindow.$('input');
                	for(var i=0;i<len;i++){
                		checks[i].checked=false;
                	}
	                $("#${id}RoleLabel").val("");
	                $("#${id}checks").val("");
                }
            }, loaded:function(h){
                $(".jbox-content", top.document).css("overflow-y","hidden");
                var checks=h.find("iframe")[0].contentWindow.$("input[name='roleIdList']");//全部数据
                for(var i=0,length=checks.length;i<length;i++){//全部数据初始化
                	checks[i].checked=false;
                }
                var cks=$("#${id}checks").val();
                if(cks.indexOf("[")>-1&&cks.indexOf("]")>-1){
                	cks=cks.substring(1,cks.length-1);
                }
                if(cks!=""){
	                cks=cks.split(",")
                }
                for(var i=0,length=checks.length;i<length;i++){
                	var boo=false;
                	var id=checks[i].value;
                	if(cks!=null&&cks.length>0){
                		for(var j=0,len=cks.length;j<len;j++){
                        	var ckId=$.trim(cks[j]);
                        	if(ckId==id){
                        		boo=true;
                        	}
                        }
                	}
                	if(boo){
                		checks[i].checked=true;
                	}
                }
            }
        });
	});
	
</script>
